#include <types.h>
#include <Debug.h>
#include <FrameAllocator.h>
#include <Paging.h>

static dword HeapStart = 0xD0000000;
static dword HeapPointer = 0xD0000000;
static dword HeapSize = 0;

void *ResizeHeap(long delta) {
	void *res = (void *) HeapPointer;

	if (delta < 0) {
	} else if (delta > 0) {
		while (delta + HeapPointer >= HeapStart + HeapSize) {
			dword tmp = FrameAllocator::Alloc(0x1000000, 1);
			dword frame = tmp ? tmp : FrameAllocator::Alloc(0x100000, 1);
			Paging::MapMemory(PAGE_FLAGS_PRESENT | PAGE_FLAGS_WRITE, HeapStart + HeapSize, frame, 0x1000);
			HeapSize += 0x1000;
		}
	}
	HeapPointer += delta;
	return res;
}
